import tkinter as tk;
import tkinter.messagebox;
from tkinter import ttk;
import keyboard,time,threading;
import pyautogui,sys,ctypes;
historical_updates={
    "V1.0.0":"  首版不涉及",
    "V1.0.3":"  修复了一些问题",
    "V1.0.5":"  完整版",
    "V2.0.0":"  窗口布局优化\n  解决了停止点击必须在鼠标点击时的问题"
}
pyautogui.PAUSE = 0;
stop = False;
def str_historical_updates():
    temp = "";
    global historical_updates;
    #将字典historical_updates全部转化为字符串
    for key,value in historical_updates.items():
        temp = temp + key + "\n" + value + "\n";#用temp变量将返回的结果装下来
    return temp;
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin();
    except:
        return False;
if is_admin():
    pass;
else:
    ctypes.windll.shell32.ShellExecuteW(None,"runas", sys.executable, __file__, None, 1);
    sys.exit();
value = 0;
def val(number):
    global value;
    value = number;
def err_show(message,title="错误"):
    tkinter.messagebox.showerror(title,message);
def thread_mainloop_stop():
    time.sleep(3);
    stop_window.destroy();
def thread_wait_press():
    global stop,stop_window;
    while keyboard.is_pressed('ctrl+t') == False:
        pass;
    stop = True;
    stop_window = tk.Tk();
    stop_window.overrideredirect(1);
    stop_window.geometry("200x40");
    stop_window.configure(bg="black");
    stop_window_text = tk.Label(stop_window,text="已停止",bg="white");
    stop_window_text.pack();
    thread_main = threading.Thread(target=thread_mainloop_stop);
    thread_main.start();
    stop_window.mainloop();
def start_():
    global stop;
    thread_wait = threading.Thread(target=thread_wait_press);
    thread_wait.start();
    while stop != True:
        if combo_selected.get() == "点击左键":
            pyautogui.click();
            time.sleep(sleep);
        if combo_selected.get() == "点击右键":                                                                                  
            pyautogui.rightClick();
            time.sleep(sleep);
    stop = False;
def thread_mainloop_start():
    time.sleep(3);
    start_window.destroy();
def thread_start():
    global start_window;
    start_window = tk.Tk();
    start_window.overrideredirect(1);
    start_window.geometry("200x40");
    start_window.configure(bg="black");
    start_window_text = tk.Label(start_window,text="已开始",bg="white");
    start_window_text.pack();
    thread_main = threading.Thread(target=thread_mainloop_start);
    thread_main.start();
    start_window.mainloop();
def start():
    global sleep;
    while True:
        while keyboard.is_pressed('ctrl+k')==False:
            pass;
        sleep = 1/inpu;
        thread_start_window = threading.Thread(target=thread_start);
        thread_start_window.start();
        start_();
def get():
    global inpu;
    global entered;
    try:
        inpu=int(entered.get());
    except:
        err_show("你输入的数值不符合要求，请重新输入");
        return None;
    if combo_selected.get() != "点击左键" and combo_selected.get() != "点击右键":
        err_show("你输入的数值不符合要求，请重新输入");
        return None;    
    if inpu <= 1000 == False or inpu >=1 == False:
        err_show("你输入的数值不符合要求，请重新输入");
    else:
        massage = "你填写的数："+str(inpu)+"    你选择点击的键："+combo_selected.get()[2:3]+"\n按下 contral+k 开始   按下 contral+t 停止";
        lb_3 = tk.Label(window,text=massage,width=34,height=4,fg="black");
        lb_3.grid(row=3,column=0,padx=(0,0),pady=0,columnspan=2)
        thread_click = threading.Thread(target=start);
        thread_click.start();
def func_top_floor():
    global top_floor,window
    if top_floor == False:
        top_floor = True;
        window.attributes('-topmost', 'true');
    else:
        top_floor = False;
        window.attributes('-topmost', 'false');
def func_about_exit():
    about_window.destroy();
def func_history_exit():
    history_window.destroy();
def func_about():
    global about_window;
    about_window = tk.Tk();
    about_window.title("关于连点器");
    about_window.geometry("250x150");
    about_window.resizable(False,False);
    about_window.attributes("-toolwindow", 2);
    lb_about = tk.Label(about_window,text="鼠标连点器  V2.0.0\n-2023年12月3日-\n本次更新内容：\n1、窗口布局优化\n2、解决了"+
                        "停止点击必须在鼠标点击时的问题",justify='left');
    bt_about_update = tk.Button(about_window,text="历史更新记录",command=history);
    bt_about_exit = tk.Button(about_window,text="关闭",command=func_about_exit);
    lb_about.grid(row=0,column=0,padx=(0,0),pady=0,columnspan=2);
    bt_about_exit.grid(row=1,column=1,padx=(0,0),pady=0);
    bt_about_update.grid(row=1,column=0,padx=(0,0),pady=0);
    about_window.mainloop();
def history(): 
    global history_window;
    history_window = tk.Tk();
    history_window.title("历史更新记录");
    history_window.geometry("400x210");
    history_window.resizable(False,False);
    history_window.attributes("-toolwindow", 2);
    lb_history = tk.Label(history_window,text=str_historical_updates(),anchor='w',justify='left');
    bt_history_exit = tk.Button(history_window,text="关闭",command=func_history_exit);
    lb_history.pack();
    bt_history_exit.pack();
    history_window.mainloop();
window = tk.Tk();
window.title("连点器");
window.geometry("270x150");
window.resizable(False,False);
top_floor = False;
top = tk.Menu(window);
top.add_cascade(label="置顶窗口/取消",command=func_top_floor);
top.add_cascade(label="关于",command=func_about);
window.config(menu=top);
lb = tk.Label(window,text="每秒次数(在1-1000内)：",width=20,height=1,fg="black");
combo_selected = tkinter.StringVar();
combo = ttk.Combobox(window, textvariable=combo_selected,width=7);
combo.configure(state="readonly");
combo["value"] = ("点击左键","点击右键");
entered = tkinter.StringVar();
text = tk.Entry(window,width=4,textvariable=entered);
bt = tk.Button(text="输入好了",width=7,command=get,fg="black");
lb_2 = tk.Label(window,text="选择点击左键/右键：",width=24,height=2,fg="black");
lb.grid(row=0,column=0,padx=(0,0),pady=0);
text.grid(row=0,column=1,padx=(0,0),pady=0,sticky="W");
combo.grid(row=1,column=1,padx=(0,0),pady=0,sticky="W");
bt.grid(row=2,column=0,padx=(0,0),pady=0,columnspan=2);
lb_2.grid(row=1,column=0,padx=(0,0),pady=0);
tk.mainloop();
